package uk.co.cdal.cobweb.frontend.GUI;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import sun.awt.HorizBagLayout;
import uk.co.cdal.cobweb.frontend.GUI.actionlisteners.NewMessageKeyboardListener;
import uk.co.cdal.cobweb.frontend.GUI.actionlisteners.NewMessageMouseListener;

public class ChatPanel extends JPanel {

	private JScrollPane sp;

	private GridLayout layout;

	private JTextArea text;

	private JTextArea messageHistory;

	public ChatPanel() {
		init();
	}

	public void init() {
		setLayout(new GridLayout(2,1));
		messageHistory = new JTextArea();
		messageHistory.setLineWrap(true);
		messageHistory
				.setText("This is your message history pane, here you will be able to view messages that have been sent to you \n");
		messageHistory.setRows(5);
		messageHistory.setMaximumSize(messageHistory.getSize());
		JScrollPane messageScrollPane = new JScrollPane(messageHistory);
		text = new JTextArea(
				"Enter your message here to begin discussing the map...");
		text.setLineWrap(true);
		text.setRows(2);
		JScrollPane inputScrollPane = new JScrollPane(text);
		JButton submit = new JButton("Send");
		JLabel submitLabel = new JLabel("Message: ");
		JPanel input = new JPanel();
		text.addKeyListener(new NewMessageKeyboardListener());
		submit.addMouseListener(new NewMessageMouseListener());
		input.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.weightx = 0.0;
		c.anchor = GridBagConstraints.LINE_START;
		input.add(submitLabel, c);
		
		c.gridx = 1;
		c.weightx = 1.0;
		c.fill = GridBagConstraints.HORIZONTAL;
		input.add(inputScrollPane, c);
		
		c.gridx = 2;
		c.weightx = 0.0;
		c.anchor = GridBagConstraints.LINE_END;
		input.add(submit, c);
		
		add(messageScrollPane);
		add(input);

	}

	public void addChatItem(ChatItem ci) {
		messageHistory.setText(messageHistory.getText() + ci.getFrom()
				+ " said @ " + ci.getDate() + "\n" + ci.getMessage() + "\n");
		// layout.setRows(layout.getRows() + 1);
		// sp.add(ci);
	}
	
	public void addSystemMessage(String message) {
		messageHistory.setText(messageHistory.getText() + "***" + message + "***\n");
	}

	public String getInput() {
		return text.getText();
	}

	public void clearInput() {
		text.setText("");
	}
}